package com.sgb.knowledgeshare.viewmodel;

import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import androidx.lifecycle.ViewModel;

import com.alibaba.sdk.android.vod.upload.VODUploadCallback;
import com.alibaba.sdk.android.vod.upload.VODUploadClientImpl;
import com.alibaba.sdk.android.vod.upload.model.UploadFileInfo;
import com.alibaba.sdk.android.vod.upload.model.VodInfo;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.view.entity.AliProofEntity;
import com.sgb.knowledgeshare.ShareConfig;
import com.sgb.knowledgeshare.entity.TabelListEntity;
import com.sgb.knowledgeshare.view.activity.SharePublishActivity;
import com.sgb.knowledgeshare.view.activity.VideoReportActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.entity.VideoTypeListEntity;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by fq on 2020/9/15 0015
 * Description：
 */
public class SharePublishActivityModel extends ViewModel {

    private  VODUploadClientImpl mUploadClient;
    SharePublishActivity activity;
    private AliProofEntity mProofEntity;
    VideoReportActivity videoreportactivity;
    public void setActivity(SharePublishActivity SharePublishActivity) {
        activity = SharePublishActivity;
        mUploadClient = new VODUploadClientImpl(activity.getApplicationContext());
    }
//    获取视频上传凭证
    public void getVideoParamsSTS(String path, EditText sharePublicText) {
        Map map = new HashMap();
        ShareConfig.getInstance().getApi().getVideoParams(map).enqueue(new ICallBack<BaseEntity<AliProofEntity>>() {
            @Override
            public void onSuccess(BaseEntity<AliProofEntity> data) {
                if (data.getState().equals("ok")){
                    mProofEntity = data.getData();
                    VodInfo vodInfo = new VodInfo();
                    vodInfo.setTitle(sharePublicText.getText().toString());
                    vodInfo.setDesc("");
                    vodInfo.setCateId((int) System.currentTimeMillis());
                    vodInfo.setTags(null);
                    mUploadClient.addFile(path, vodInfo);
                    //阿里凭证
                    mUploadClient.init(mUploadCallback);
                    mUploadClient.start();
                }
            }
        });
    }
    VODUploadCallback mUploadCallback = new VODUploadCallback() {
        public void onUploadSucceed(UploadFileInfo info) {
            Log.e("onUploadSucceed", "onUploadSucceed");
            activity.setAliInfoVideoParamsSTS(mProofEntity.getVideoId(),info);
           // mActivity.updateVideoInfo(mProofEntity.getVideoId(), info.getFilePath());

        }

        public void onUploadFailed(UploadFileInfo info, String code, String message) {
            Log.e("onUploadFailed", "onUploadFailed");
//            OSSLog.logError("onfailed ------------------ " + info.getFilePath() + " " + code + " " + message);
        }

        public void onUploadProgress(UploadFileInfo info, long uploadedSize, long totalSize) {
            Log.e("onUploadFailed", "onUploadFailed");
//            OSSLog.logDebug("onProgress ------------------ " + info.getFilePath() + " " + uploadedSize + " " + totalSize);
        }

        public void onUploadTokenExpired() {
            Log.e("onUploadTokenExpired", "onUploadTokenExpired");
//            OSSLog.logError("onExpired ------------- ");
            // 重新刷新上传凭证:RefreshUploadVideo
//            uploadAuth = "此处需要设置重新刷新凭证之后的值";
            mUploadClient.resumeWithAuth(mProofEntity.getUploadAuth());
        }

        public void onUploadRetry(String code, String message) {
            Log.e("onUploadRetry", "onUploadRetry");
//            OSSLog.logError("onUploadRetry ------------- ");
        }

        public void onUploadRetryResume() {
            Log.e("onUploadRetryResume", "onUploadRetryResume");
//            OSSLog.logError("onUploadRetryResume ------------- ");
        }

        public void onUploadStarted(UploadFileInfo uploadFileInfo) {
            Log.e("onUploadStarted", "onUploadStarted");
//            OSSLog.logError("onUploadStarted ------------- ");
            mUploadClient.setUploadAuthAndAddress(uploadFileInfo, mProofEntity.getUploadAuth(), mProofEntity.getUploadAddress());
        }
    };

//保存视频信息
    public void getSaveVideo(String imagePath, String type,String type2, String title, String videoId, LinearLayout sharePublic,String labelName,List<String> labelString) {
        Map map = new HashMap();
        map.put("cover",imagePath);//封面图
        map.put("type",type);//类型
        map.put("type2",type2);//类型
        map.put("title",title);//标题
        map.put("isRecommendCover",0);//是否推荐封面0是1否
        map.put("videoId",videoId);//视频id
        map.put("labelName",labelName);
        map.put("labelList",labelString);
        ShareConfig.getInstance().getApi().getsaveVideo(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals("ok")){
                    activity.setSucceed();
                }
            }
            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                sharePublic.setSelected(true);
            }
        });
    }

    //上传多图片
    public void uploadMutiFileModel(File file) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"image.jpg\"", requestBody);
        ShareConfig.getInstance().getApi().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(ShareConfig.RESPONSE_OK)){
                    activity.uploadRefresh(data.getData().toString());
                }
            }
        });
    }
//    获取发布类型
    public void getvideoTypeList() {
        ShareConfig.getInstance().getApi().getCategoryTree().enqueue(new ICallBack<BaseEntity<List<VideoTypeListEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<VideoTypeListEntity>> data) {
                if (data==null)return;
                if (data.getState().equals("ok")){
                    activity.setVideoTypeList(data.getData());
                }
            }
        });

    }

    //搜索对应的标签列表信息
    public void getLabelList(String searchName) {
        Map map = new HashMap();
        map.put("labelName", searchName);
        ShareConfig.getInstance().getApi().getLabelList(map).enqueue(new ICallBack<BaseEntity<List<TabelListEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<TabelListEntity>> data) {
                if (data.getData()==null)return;
                if (data.getState().equals("ok")){
                    activity.setLabelList(data.getData());
                }
            }
        });
    }

    //添加标签弹窗确定后传递数据
    public void setLabelName(String labelName, String labelNo) {
        TabelListEntity tabellistentity= new TabelListEntity();
        tabellistentity.setLabelName(labelName+"");
        tabellistentity.setLabelNo(labelNo+"");
       activity.setLabelName(tabellistentity);
    }

    //获取当前用户标签列表
    public void getLabelByUserNO() {
        Map map = new HashMap();
        map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO,""));
        ShareConfig.getInstance().getApi().getLabelByUserNO(map).enqueue(new ICallBack<BaseEntity<List<TabelListEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<TabelListEntity>> data) {
                if (data.getData()==null)return;
                if (data.getState().equals("ok")){
                    activity.setUserLabelList(data.getData());
                }
            }
        });
    }
}
